/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Global;

import Main.GameOptionState;
import Main.MusicFactory;
import Main.MusicFactoryDefault;
import Main.MusicFactoryMute;
import Main.SoundFactory;
import Main.SoundFactoryDefault;
import Main.SoundFactoryMute;
import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Huy
 */
public class GlobalGameOption {

	public static MusicFactory MUSIC;
	public static SoundFactory SOUND;
	public static Point[] RESOLUTION = new Point[]{
		new Point(800, 600),
		new Point(1024, 768)};
	//new Point(1366, 768)};
	private static GlobalGameOption option;
	public String texturePackage;
	public String playerName;
	public boolean isMusic;
	public boolean isSound;
	public int resolution;
	public boolean isFullScreen;
	public int speed;

	private GlobalGameOption() {
	}

	public static GlobalGameOption getOption() {
		if (option == null) {
			option = new GlobalGameOption();
		}
		return option;
	}

	public void setDefault() {
		texturePackage = "TexturePackages/DefaultPackage";
		playerName = "Player";
		isMusic = true;
		isSound = true;
		resolution = 0;
		isFullScreen = true;
		speed = 1;
	}

	public void readXML() {

		File fXmlFile = new File("Option.xml");
		if (fXmlFile.exists()) {
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = null;
			Document doc = null;
			try {
				dBuilder = dbFactory.newDocumentBuilder();
			} catch (ParserConfigurationException ex) {
				Logger.getLogger(GameOptionState.class.getName()).log(Level.SEVERE, null, ex);
			}

			try {
				doc = dBuilder.parse(fXmlFile);
			} catch (SAXException ex) {
				Logger.getLogger(GameOptionState.class.getName()).log(Level.SEVERE, null, ex);
			} catch (IOException ex) {
				Logger.getLogger(GameOptionState.class.getName()).log(Level.SEVERE, null, ex);
			}

			doc.getDocumentElement().normalize();
			NodeList nList = doc.getElementsByTagName("GamePlay");
			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;
					speed = getTagValueInt("GameSpeed", eElement);
					playerName = getTagValue("PlayerName", eElement);
					
				}
			}
			nList = doc.getElementsByTagName("Screen");
			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;
					resolution = getTagValueInt("Resolution", eElement);
					texturePackage = getTagValue("ThemeName", eElement);
					isFullScreen = getTagValueBool("FullScreen", eElement);

				}
			}
			nList = doc.getElementsByTagName("Sound");
			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;
					isSound = getTagValueBool("SoundEffect", eElement);
					isMusic = getTagValueBool("Music", eElement);

				}
			}
		} else {
			setDefault();
		}
		refresh();
	}
	public void refresh()
	{
		if(isMusic)
		{
			MUSIC = new MusicFactoryDefault();
			
		}else
		{
			MUSIC = new MusicFactoryMute();
		}
		if(isSound)
		{
			SOUND = new SoundFactoryDefault();
			
		}else
		{
			SOUND = new SoundFactoryMute();
		}
	}

	public void writeXML() throws TransformerConfigurationException, TransformerException {
		try {

			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();


			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("Option");
			doc.appendChild(rootElement);

			Element gamePlay = doc.createElement("GamePlay");
			rootElement.appendChild(gamePlay);


			Element gameSpeed = doc.createElement("GameSpeed");
			gameSpeed.appendChild(doc.createTextNode("" + speed));
			gamePlay.appendChild(gameSpeed);

			Element name = doc.createElement("PlayerName");
			name.appendChild(doc.createTextNode(playerName));
			gamePlay.appendChild(name);

			Element screen = doc.createElement("Screen");
			rootElement.appendChild(screen);

			Element resolutionE = doc.createElement("Resolution");
			resolutionE.appendChild(doc.createTextNode("" + resolution));
			screen.appendChild(resolutionE);

			Element theme = doc.createElement("FullScreen");
			theme.appendChild(doc.createTextNode("" + isFullScreen));
			screen.appendChild(theme);

			Element themename = doc.createElement("ThemeName");
			themename.appendChild(doc.createTextNode(texturePackage));
			screen.appendChild(themename);

			Element sound = doc.createElement("Sound");
			rootElement.appendChild(sound);

			Element soundEffect = doc.createElement("SoundEffect");
			//if (isSound) {
				soundEffect.appendChild(doc.createTextNode("" + isSound));
			/*} else {
				soundEffect.appendChild(doc.createTextNode("" + 0));
			}*/

			sound.appendChild(soundEffect);

			Element music = doc.createElement("Music");
			//if (isMusic) {
				music.appendChild(doc.createTextNode("" + isMusic));
			/*} else {
				music.appendChild(doc.createTextNode("" + 0));
			}*/
			sound.appendChild(music);

			// write the content into xml file
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new File("Option.xml"));

			// Output to console for testing
			// StreamResult result = new StreamResult(System.out);

			transformer.transform(source, result);

			System.out.println("File saved!");

		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		} catch (TransformerException tfe) {
			tfe.printStackTrace();
		}
	}

	private String getTagValue(String sTag, Element eElement) {
		NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

		Node nValue = (Node) nlList.item(0);

		return nValue.getNodeValue();
	}
	private boolean getTagValueBool(String sTag, Element eElement) {
		return Boolean.parseBoolean(getTagValue(sTag, eElement));
	}

	private int getTagValueInt(String sTag, Element eElement) {
		return Integer.parseInt(getTagValue(sTag, eElement));
	}
}
